﻿Imports System.Data.OleDb

Public Class ImageGal
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

        ' ImageIpload is the name of the upload box field. 

        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(ImageUpload.FileName)

        If ImageUpload.HasFile Then
            ImageUpload.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
        End If

        Dim sql As String = "INSERT INTO  Images (ImageLink,ImageDescription,DateUploaded,TimeUploaded,UserIDFK) VALUES (@f1,@f2,@f3,@f4,@f5)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("Mercedes1ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f2", txtdescription.Text)
        cmd.Parameters.AddWithValue("@f3", Now.Date.ToString("dd/MM/yyyy"))
        cmd.Parameters.AddWithValue("@f4", DateAndTime.Now.ToString("t"))
        cmd.Parameters.AddWithValue("@f5", Membership.GetUser(User.Identity.Name).ProviderUserKey)


        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("ImageGal.aspx")





        ' If ImageUpload.HasFile Then
        'ImageUpload.SaveAs(Request.MapPath("Pictures") & "/" & ImageUpload.FileName)

        '  End If



        ' Dim oleDbConn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MercedesConnectionString").ConnectionString)
        ' Dim SqlString As String = "Insert into Image (ImageLink,ImageDescription,DateUploaded,TimeUploaded,MemberIDFK) Values (@f1,@f2,f@3,f@4,@f5)"
        '  Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        ' oleDbConn.Open()

        ' cmd.Parameters.AddWithValue("@f1", "Pictures/" & ImageUpload.FileName)
        ' cmd.Parameters.AddWithValue("@f2", txtdescription.Text)
        ' cmd.Parameters.AddWithValue("@f3", Now.Date)
        'cmd.Parameters.AddWithValue("@f4", DateAndTime.Now.ToString("dd/MM/yyyy"))
        'cmd.Parameters.AddWithValue("@f5", User.Identity.Name)




        '   cmd.ExecuteNonQuery()
        ' Response.Redirect("ImageGal.aspx")

















    End Sub
    ' Make sure it does not have the same file name. 

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    
End Class